package com.example.ffepeg.ffpeg;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * 图片 -> MP4视频
 */
public class FfmpegImageToVideoConverter {
    public static void main(String[] args) {
        ffmpegImageToVideo();
    }

    public static void ffmpegImageToVideo() {
        long startTime =System.currentTimeMillis();
        List commend = new ArrayList();
        commend.add("D:\\WorkSoftware\\ffmpeg\\ffmpeg-7.0.1-essentials_build\\ffmpeg-7.0.1-essentials_build\\bin\\ffmpeg.exe");
        //以1fps读入
        commend.add("-r");
        commend.add("5");   //帧率
        //-threads 2 以两个线程进行运行， 加快处理的速度。
        commend.add("-threads");
        commend.add("2");
        commend.add("-f");
        commend.add("image2");
        commend.add("-i");
        commend.add("D:\\javaXM\\FFEpeg\\src\\main\\resources\\file\\png\\frame_%04d.jpg");
        commend.add("-vcodec");
        commend.add("libx264");
        //-y 对输出文件进行覆盖
        commend.add("-y");
        //-r 5 fps设置为5帧/秒（不同位置有不同含义)  %2d标识图片文件的正则
        commend.add("-r");
        commend.add("10");
        commend.add("D:\\javaXM\\FFEpeg\\src\\main\\resources\\out\\imgMP4out.mp4");
        int start = start(commend);
        System.out.println(start);
        long endTime =System.currentTimeMillis();
        System.out.println((endTime-startTime));
    }
    private static int start(List commend) {
        Process p = null;
        try {
            ProcessBuilder builder = new ProcessBuilder(commend);
            builder.command(commend);
            p = builder.start();
            p.getOutputStream().close();
            int i = doWaitFor(p);
            System.err.println(i);
            p.destroy();
            return i;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            p.destroy();
        }
        return 0;
    }

    public static int doWaitFor(Process p) {
        InputStream in = null;
        InputStream err = null;
        int exitValue = -1;

        try {
            in = p.getInputStream();
            err = p.getErrorStream();
            boolean finished = false;

            while(!finished) {
                try {
                    Character c;
                    while(in.available() > 0) {
                        c = new Character((char) in.read());
                        System.out.print(c);
                    }

                    while(err.available() > 0) {
                        c = new Character((char)err.read());
                        System.out.print(c);
                    }

                    exitValue = p.exitValue();
                    finished = true;
                } catch (IllegalThreadStateException var19) {
                    Thread.currentThread();
                    Thread.sleep(500L);
                }
            }
        } catch (Exception var20) {
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException var18) {
            }

            if (err != null) {
                try {
                    err.close();
                } catch (IOException var17) {
                }
            }

        }
        return exitValue;
    }
}
